package org.itboys.shop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.basetemplate.entity.TravelNote;
import org.itboys.basetemplate.entity.TravelNoteCat;
import org.itboys.basetemplate.service.TravelNoteCatService;
import org.itboys.basetemplate.service.TravelNoteService;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 游记
 */
@Controller
@RequestMapping(value = "/wx/travelNote")
public class WXTravelNoteController extends BaseController{

	@Autowired
	private TravelNoteCatService catService;
	@Autowired
	private TravelNoteService noteService;	
	@Autowired
	private ResourceHolder resourceHolder;
	/**
	 * 列表页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toList")
	public ModelAndView toList(Integer page, Integer rows, Long catId, Model model){
		if (page == null) {
			page = 1;
		}
		if (rows == null) {
			rows = 10;
		}
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("type", 1);
		param.put("status", 2);
		param.put("pageNo", 1);
		param.put("pageSize", 7);
		List<TravelNoteCat> list = catService.listWithPaging(param);
		model.addAttribute("page", page);
		model.addAttribute("rows", rows);
		model.addAttribute("catId", catId);
		model.addAttribute("list", list);
		addAttribute(model);
		return new ModelAndView("travelNote/list");
	}
	/**
	 * 分页查询
	 * @param request
	 * @param response
	 */
	@RequestMapping("/list")
	public void list(Long catId, HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("catId", catId);
		param.put("type", 1);
		param.put("status", 2);
		String imgRoot = resourceHolder.getStringValue("imgRoot");
		Page<TravelNote> pageNav = noteService.pageQuery(param,request);
		List<TravelNote> list = new ArrayList<TravelNote>();
		for (TravelNote bean : pageNav.getData()) {
			if (bean!=null && bean.getImage()!=null && "".equals(bean.getImage())) {
				bean.setImage(imgRoot+bean.getImage());
				list.add(bean);
			}
		}
		JsonPageUtils.renderJsonPage(pageNav.getTotal(), list, response);
	}	
	/**
	 * 获取信息
	 * @param id
	 * @param response
	 */
	@RequestMapping("/get/{id}")
	public void get(@PathVariable("id") Long id,HttpServletResponse response){
		TravelNote bean = noteService.getById(id);
		AjaxUtils.renderJson(response, bean);
	}
	/**
	 * 攻略详情
	 * @param id
	 * @param response
	 */
	@RequestMapping(value = "/view")
	public ModelAndView detail(
			@RequestParam(value = "id", required = true) Long id, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		try {
//			Member member = (Member) request.getSession().getAttribute("user");
//			Long memberId = member.getId();
			String imgRoot = resourceHolder.getStringValue("imgRoot");
			TravelNote bean = noteService.getById(id);
			if (bean!=null && bean.getImage()!=null && "".equals(bean.getImage())) {
				bean.setImage(imgRoot+bean.getImage());
			}
			model.addAttribute("bean", bean);
			addAttribute(model);
			//AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getMsgResultDO(productDetail, "success"));
		} catch (Exception e) {
			logger.error("product detail error!", e);
			//AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getErrorResultDO(-1, "系统错误"));
		}
		return new ModelAndView("travelNote/view");
	}
}
